<?php

namespace App\Observers;

use App\Model\OrderProduct;

use App\Model\Product;

use App\Model\Order;
use App\Model\OrderChangeLog;

use App\ModeL\BistroIncomeDay;
use App\Model\BistroIncomeMonth;

use App\Model\ProductSaleDay;
use App\Model\ProductSaleMonth;


use Illuminate\Support\Facades\DB;
//use App\Model\Bistro;
class OrderProductObserver
{
    /**
     * 监听订单指派
     */
//    public function created(DeliveryNote $delivery_note)
//    {
//        $order = Order::findOrFail($delivery_note->order_id);
//        $packing_man = BackendUser::findOrFail($delivery_note->packing_man_id);
//        $delivery_man = BackendUser::findOrFail($delivery_note->deliveryman_id);
//        $logdata['user_id'] = $delivery_note->operation_man_id;
//        $logdata['user_type'] = '2';
//        $logdata['log_type']='2';//创建加盟商
//        $logdata['summary'] = '指派了任务';
//        $logdata['content']='用户成功指派'.$packing_man->name.'分发'.$order->order_on.'至送货人'.$delivery_man->name;
//        Log::create($logdata);
//    }

    public function updated(OrderProduct $order_product)
    {
        $order_id = $order_product->order_id;
        $order = Order::findorfail($order_id);
        if($order->order_status == 'ordered'){
            return;
        }

        $order_products = OrderProduct::where('order_id', $order_id)->where('status', '<>', 'return')->get();
        foreach ($order_products as $od) {
            if($od->status != 'done'){
                \Log::info('菜未上齐'.date('Y-m-d H:i:s'));
                return;
            }
        }
        $order->order_status = 'ordered';
        $order->save();
        \Log::info('菜已上齐'.date('Y-m-d H:i:s'));

    }

}